package com.web.base.auth.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.blue.jdbc.Page;
import com.web.base.auth.model.App;
import com.web.base.auth.message.AppRequest;
import com.web.base.auth.message.AppResponse;

/**
 * 应用转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月15日
 */
public class AppConvertor
{
	private AppConvertor()
	{
	}
	
	public static App toApp(AppRequest request)
	{
		if (request == null)
			return null;
		
		App app = new App();
		app.setId(request.getId());
		app.setCode(request.getCode());
		app.setName(request.getName());
		app.setDescp(request.getDescp());
		app.setState(request.getState());
		return app;
	}
	
	public static AppResponse toResponse(App app)
	{
		if (app == null)
			return null;
		
		AppResponse response = new AppResponse();
		response.setId(app.getId());
		response.setCode(app.getCode());
		response.setName(app.getName());
		response.setDescp(app.getDescp());
		response.setState(app.getState());
		return response;
	}
	
	public static List<AppResponse> toResponse(List<App> list)
	{
		List<AppResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (App app : list)
		{
			responseList.add(toResponse(app));
		}
		return responseList;
	}
	
	public static Map<Integer, AppResponse> toResponse(Map<Integer, App> map)
	{
		Map<Integer, AppResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, App> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue()));
		}
		return responseMap;
	}
	
	public static Page<AppResponse> toResponse(Page<App> page)
	{
		if (page == null)
			return null;
		
		Page<AppResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList()));
		return responsePage;
	}
	
}
